﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoundEdge.Media.Wave;
using System.ComponentModel.Composition;
using SoundEdge.Media.Settings;
using SoundEdge.WinControls.Settings;
using SoundEdge.Media;

namespace SoundEdge.WinControls.WavePlayer
{
    public partial class WavePlayer : UserControl
    {
        //private Action<float> setVolumeDelegate;

        public IOutputDevicePlugin OutputPlugin { get { return audioPlayer.OutputPlugin; } set { audioPlayer.OutputPlugin = value; } }

        public WaveStream Stream { get { return audioPlayer.Stream; } }
        public event DeviceStatusChangeEventHandler DeviceStatusChange;
        public IWaveProvider Provider
        {
            get { return audioPlayer.Provider; }
        }
        public WavePlayer()
        {
            InitializeComponent();
            audioPlayer.DeviceStatusChange += new Media.DeviceStatusChangeEventHandler(audioPlayer_DeviceStatusChange);
            audioPlayer.Message += new Media.MessageEventHandler(audioPlayer_Message);
            audioPlayer.TimeElapsed += new TimeElapsedEventHandler(audioPlayer_TimeElapsed);
            audioPlayer.PositionChange += new EventHandler(audioPlayer_PositionChange);
            audioPlayer.StreamMetter += new EventHandler<StreamMetterEventArgs>(audioPlayer_StreamMetter);
        }

        void audioPlayer_StreamMetter(object sender, StreamMetterEventArgs e)
        {
            
        }

        void audioPlayer_PositionChange(object sender, EventArgs e)
        {
            //currentTimeLabel.Text = Stream.Position.ToString();
        }

        void audioPlayer_TimeElapsed(object sender, TimeElapsedEventArgs e)
        {
            currentTimeLabel.Text = String.Format("{0:00}:{1:00} of {2:00}:{3:00}", e.Elapsed.Minutes, e.Elapsed.Seconds, e.Total.Minutes, e.Total.Seconds);
            audioProgressBar.Value = (int)e.Elapsed.TotalSeconds;
        }

        void audioPlayer_Message(object sender, MessageEventArgs e)
        {
            switch (e.MessageType)
            {
                case SoundEdge.Common.MessageType.Normal:
                    MessageBox.Show(e.Message, "Audio player", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case SoundEdge.Common.MessageType.Warning:
                    MessageBox.Show(e.Message, "Audio player", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    break;
                case SoundEdge.Common.MessageType.Error:
                    MessageBox.Show(e.Message, "Audio player", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case SoundEdge.Common.MessageType.Success:
                    MessageBox.Show(e.Message, "Audio player", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default:
                    break;
            }
        }

        void audioPlayer_DeviceStatusChange(object sender, Media.DeviceStatusChangeEventArgs e)
        {
            toolStrip.Enabled = e.State != Media.PlayerState.NA;
            if (e.MediaLength != TimeSpan.Zero)
            {
                currentTimeLabel.Text = String.Format("00:00 of {0:00}:{1:00}", e.MediaLength.TotalMinutes, e.MediaLength.Seconds);
                audioProgressBar.Maximum = (int)e.MediaLength.TotalSeconds;
            }
            InitializeControls(e.State);
            if (DeviceStatusChange != null)
            {
                DeviceStatusChange(this, e);
            }
        }

        private void InitializeControls(PlayerState state)
        {
            toolStrip.Enabled = state != PlayerState.NA;

            if (!toolStrip.Enabled)
            {
                return;
            }

            openToolStripButton.Enabled = state >= PlayerState.Ready;
            playToolStripButton.Enabled = state > PlayerState.Ready;
            pauseToolStripButton.Enabled = state > PlayerState.Ready;
            stopToolStripButton.Enabled = state > PlayerState.Ready;
            loopToolStripButton.Enabled = state > PlayerState.Ready;
            
            playToolStripButton.Checked = state == PlayerState.Playing;
            pauseToolStripButton.Checked = state == PlayerState.Paused;
            stopToolStripButton.Checked = state == PlayerState.Stopped;
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            string allExtensions = string.Join(";", (from f in audioPlayer.InputFileFormats select "*" + f.Extension).ToArray());
            openFileDialog.Filter = String.Format("All Supported Files|{0}|All Files (*.*)|*.*", allExtensions);
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                audioPlayer.FileName = openFileDialog.FileName;
            }
        }

        private void playToolStripButton_Click(object sender, EventArgs e)
        {
            audioPlayer.Play();
        }

        private void pauseToolStripButton_Click(object sender, EventArgs e)
        {
            audioPlayer.Pause();
        }

        private void stopToolStripButton_Click(object sender, EventArgs e)
        {
            audioPlayer.Stop();
        }

        private void audioProgressBar_MouseClick(object sender, MouseEventArgs e)
        {
            if (audioPlayer.State >= PlayerState.StreamReady)
            {
                double percent = e.X * 100 / audioProgressBar.Width;
                audioProgressBar.Value = (int)Math.Floor(audioProgressBar.Maximum * percent / 100);
                audioPlayer.SetPosition(audioProgressBar.Value);
            }
        }

    }
}
